<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
              <el-form-item label="平台小程序ID" prop="appid">
        <el-input v-model="dataForm.appid" placeholder="平台小程序ID"></el-input>
      </el-form-item>
          <el-form-item label="是否显示(0:否;1:是)" prop="display">
        <el-input v-model="dataForm.display" placeholder="是否显示(0:否;1:是)"></el-input>
      </el-form-item>
          <el-form-item label="排序" prop="sort">
        <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
                      <el-form-item label="Logo" prop="logo">
        <el-input v-model="dataForm.logo" placeholder="Logo"></el-input>
      </el-form-item>
          <el-form-item label="联系方式" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="联系方式"></el-input>
      </el-form-item>
          <el-form-item label="地理经度(简写)" prop="lng">
        <el-input v-model="dataForm.lng" placeholder="地理经度(简写)"></el-input>
      </el-form-item>
          <el-form-item label="地理纬度(简写)" prop="lat">
        <el-input v-model="dataForm.lat" placeholder="地理纬度(简写)"></el-input>
      </el-form-item>
          <el-form-item label="国家" prop="country">
        <el-input v-model="dataForm.country" placeholder="国家"></el-input>
      </el-form-item>
          <el-form-item label="省份" prop="province">
        <el-input v-model="dataForm.province" placeholder="省份"></el-input>
      </el-form-item>
          <el-form-item label="城市" prop="city">
        <el-input v-model="dataForm.city" placeholder="城市"></el-input>
      </el-form-item>
          <el-form-item label="地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="地址"></el-input>
      </el-form-item>
          <el-form-item label="营业时间" prop="businesshours">
        <el-input v-model="dataForm.businesshours" placeholder="营业时间"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import API from '@/api'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
                  id: '',
                          appid: '',
                          display: '',
                          sort: '',
                                      logo: '',
                          phone: '',
                          lng: '',
                          lat: '',
                          country: '',
                          province: '',
                          city: '',
                          address: '',
                          businesshours: '',
                },
        dataRule: {
                  appid: [
            { required: true, message: '平台小程序ID不能为空', trigger: 'blur' }
          ],
              display: [
            { required: true, message: '是否显示(0:否;1:是)不能为空', trigger: 'blur' }
          ],
              sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
                          logo: [
            { required: true, message: 'Logo不能为空', trigger: 'blur' }
          ],
              phone: [
            { required: true, message: '联系方式不能为空', trigger: 'blur' }
          ],
              lng: [
            { required: true, message: '地理经度(简写)不能为空', trigger: 'blur' }
          ],
              lat: [
            { required: true, message: '地理纬度(简写)不能为空', trigger: 'blur' }
          ],
              country: [
            { required: true, message: '国家不能为空', trigger: 'blur' }
          ],
              province: [
            { required: true, message: '省份不能为空', trigger: 'blur' }
          ],
              city: [
            { required: true, message: '城市不能为空', trigger: 'blur' }
          ],
              address: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
              businesshours: [
            { required: true, message: '营业时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            API.ststoreinfo.info(this.dataForm.id).then(({data}) => {
              if (data && data.code === 0) {
                        this.dataForm.appid = data.stStoreInfo.appid
                    this.dataForm.display = data.stStoreInfo.display
                    this.dataForm.sort = data.stStoreInfo.sort
                                this.dataForm.logo = data.stStoreInfo.logo
                    this.dataForm.phone = data.stStoreInfo.phone
                    this.dataForm.lng = data.stStoreInfo.lng
                    this.dataForm.lat = data.stStoreInfo.lat
                    this.dataForm.country = data.stStoreInfo.country
                    this.dataForm.province = data.stStoreInfo.province
                    this.dataForm.city = data.stStoreInfo.city
                    this.dataForm.address = data.stStoreInfo.address
                    this.dataForm.businesshours = data.stStoreInfo.businesshours
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
                  'id': this.dataForm.id,
                  'appid': this.dataForm.appid || undefined,
                      'display': this.dataForm.display || undefined,
                      'sort': this.dataForm.sort || undefined,
                      'logo': this.dataForm.logo || undefined,
                      'phone': this.dataForm.phone || undefined,
                      'lng': this.dataForm.lng || undefined,
                      'lat': this.dataForm.lat || undefined,
                      'country': this.dataForm.country || undefined,
                      'province': this.dataForm.province || undefined,
                      'city': this.dataForm.city || undefined,
                      'address': this.dataForm.address || undefined,
                      'businesshours': this.dataForm.businesshours || undefined,
                }
            var tick = !this.dataForm.id ? API.ststoreinfo.add(params) : API.ststoreinfo.update(params)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
